<template>
  <div class="addExpert">
    <!--导航-->
    <tmpHeader :index='4'/>
    <h2 style="text-align:center;font-weight:600;margin-bottom:10px;color: #333">严选专家新增</h2>
    <div class="main_expert">
      <p class="title">专家基本信息</p>
      <Row gutter="24" class="box">
        <Col span="8">
          <!--专家姓名-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span class="hongdian">&nbsp;&nbsp;*</span>
              <span>选择医生：</span>
            </div>
            <AutoComplete
              v-model="name"
              @on-search="search"
              :data='list'
              placeholder="请输入专家姓名查询"
    
              @on-select="onSelect"
              style="width:500px">
                <Option v-for="option,index in list" @click.stop.native="onChange(option)" :value="option.name" :key="option.id" :class="{'added': option.iexpert && option.iexpert == 1, 'add': !option.iexpert || option.iexpert == 0}" >
                    <span class="demo-auto-complete-title doctor">{{ option.name }}</span>
                    <span class="demo-auto-complete-title doctor">{{ option.deptName }}</span>
                    <span class="demo-auto-complete-title doctor">{{ option.titleName }}</span>
                    <span class="demo-auto-complete-title doctor">{{ option.phone }}</span>
                    <span class="demo-auto-complete-title" v-if="option.iexpert && option.iexpert == 1 ">已添加</span>
                    <span class="demo-auto-complete-title" v-if="!option.iexpert || option.iexpert == 0 ">添加</span>
                </Option>
              </AutoComplete>
          </div>         
        </Col>
        <Col span="12">
          <div class="main_expert_item">
            <!-- <Button type="primary"  icon="ios-search" @click="search">查询</Button> -->
          </div>           
        </Col>
        <Col span="8">
          <!--医生姓名-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>医生姓名：</span>
            </div>
            <Input v-model="name" placeholder="空" style="width: 500px" disabled/>
          </div>
          <!--机构名称-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>所属机构：</span>
            </div>
            <Input v-model="yname" placeholder="该专家所在机构名称" style="width: 500px" disabled/>
          </div>
          <!--是否为专家-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>专家标识：</span>
            </div>
            <iSwitch size="large"  v-model="switch2" @on-change="changeStatus(switch2)">
                <span slot="open">开启</span>
                <span slot="close">关闭</span>
            </iSwitch>
          </div>
        </Col>
        <Col span="8">
          <!--医生性别-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>医生性别：</span>
            </div>
            <Input v-model="gender" placeholder="空" style="width: 500px" disabled/>
          </div>
          <!--专家科室-->
          <div class="main_expert_experts">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>所属科别：</span>
            </div>
            <div class="iselected">
              <Select v-model="keshi" style="width:200px;margin-right:10px" disabled>
                <Option :value="keshi" :key="keshi">{{ keshi }}</Option>
              </Select>
            </div>
          </div>
          <!--显示-->
          <div class="main_expert_item">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>是否在公众端显示：</span>
            </div>
            <iSwitch size="large"  v-model="switch1" :disabled="switch1Disable">
                <span slot="open">开启</span>
                <span slot="close">关闭</span>
            </iSwitch>
          </div>
        </Col>
        <Col span="8">
            <!--联系电话-->
            <div class="main_expert_item">
              <div class="main_expert_title">
                <span style="color:red;">&nbsp;&nbsp;</span>
                <span>联系电话：</span>
              </div>
              <Input v-model="phone" placeholder="空" style="width: 500px" disabled/>
            </div>
            <!--职务-->
            <div class="main_expert_item">
              <div class="main_expert_title">
                <span style="color:red;">&nbsp;&nbsp;</span>
                <span>医师职称：</span>
              </div>
              <Select v-model="zhiwu" style="width:200px" disabled>
                <Option :value="zhiwu" :key="zhiwu">{{ zhiwu }}</Option>
                <!-- <Option
                  v-for="(item) in doctorList"
                  :value="item.value"
                  :key="item.value"
                >{{ item.value }}</Option> -->
              </Select>
              <!-- 职务 -->
              <!-- <Input v-model="post" placeholder="请输入专家职务" style="width: 200px"/> -->
            </div>
            <!--排序-->
            <div class="main_expert_item">
              <div class="main_expert_title">
                <span style="color:red;">&nbsp;&nbsp;</span>
                <span>列表排序：</span>
              </div>
              <InputNumber :max="99999" :min="1" v-model.trim="isort" style="width: 100px" placeholder="输入数字"></InputNumber>
              <small style="margin-left:10px;color:#FF9966">备注:只能填写数字,1代表置顶以此类推</small>
            </div>
        </Col>
      </Row>
    </div>
    <div class="main_expert">     
      <p class="title">专家附属信息</p>
      <Row class="box">
        <Col span="12">
          <!--专业特长-->
          <div class="main_expert_inputi">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>医生擅长：</span>
            </div>
            <div class="shuru">
              <Input
                v-model="expert1"
                type="textarea"
                disabled
                :rows="6"
                placeholder="专家专业特长"
              />
            </div>
          </div>
        </Col>
        <Col span="12">
          <!--个人简介-->
          <div class="main_expert_inputi">
            <div class="main_expert_title">
              <span style="color:red;">&nbsp;&nbsp;</span>
              <span>医生简介：</span>
            </div>
            <div class="shuru">
              <Input
                v-model="expert2"
                type="textarea"
                disabled
                :rows="6"
                placeholder="专家个人简介"
              />
            </div>
          </div>
        </Col>
      </Row>
    </div>
    <!--保存-->
    <div class="expert_save">
      <Button type="primary" @click="save" :disabled='disabledSave' style="width:75px;height:35px;">
        <Icon type="ios-checkmark" style="margin-right: 5px" />保存
      </Button>
      <Button @click="back" style="width:75px;height:35px;">
        <i class = "iconfont icon-butongyi" style="font-size:14px;margin-right:5px"></i>  取消
      </Button>
    </div>
  </div>
</template>
<script>
import tmpHeader from "@/pages/organizat/contentmen/tmpHeader";
import api from "@/api/commonApi";
import {AutoComplete} from 'view-design'
export default {
  components: {
    tmpHeader,
    AutoComplete
  },
  data() {
    return {
      id: "tinymce-editor",
      id1: "tinymce-editor1",
      height: 200,
      tinymceHtml: "",
      switch1: false,
      switch1Disable: false,   ////禁止公众端显示点击
      name: "",
      yname: "",
      keshi: "",
      zhiwu: "",
      isort: "",
      expert1: "",
      expert2: "",
      post: "",
      id: sessionStorage.getItem("hospitalId"),
      list: [],
      yKeshi: "",
      currentId: -1,
      switch2: false,
      doctorList: [
        {
          value: "主任医师"
        },
        {
          value: "副主任医师"
        },
        {
          value: "住院医师"
        },
        {
          value: "主治医师"
        }
      ],
      disabledSave: false,
      data: {},
    };
  },
  created() {
    if(this.switch2 ==  false){
      this.switch1 =  false
      this.switch1Disable = true
    } else {
        this.switch1Disable = false           
    }
    let breadList = [
      { path: "/index", title: "首页" },
      {
        path: "/index/operation/mechanism/index",
        title: "机构运营"
      },
      {
        path: "/index/operation/home",
        title: "机构配置管理"
      }
    ];
    this.$emit("changeBreadList", breadList);
  },
  mounted(){
    this.status()
  },
  methods: {
    //根据专家标识状态控制公众端按钮点击情况
    changeStatus(val){
      if(val == false){
          this.switch1 =  false
          this.switch1Disable = true              
      } else {
          this.switch1Disable = false           
      }
    },
    status() {
      let flag = localStorage.getItem("status");
      if (!Boolean(flag)) {
        this.$Message.info("您还没有开通互联网医院,去开通");
        this.flag = true;
        localStorage.setItem("homeIndex", 0);
        setTimeout(() => {
          this.functionJS.paramsNavgationTo(this, "homeinfo", {
            // 公用方法
          }); 
        }, 600);
      }
    },
    isorts() {
      if (this.isort <= 0) {
        this.isort = "";
        this.$Message.info("排序置顶为1");
      }
    },
    // 加载医生详情
    changes() {
      if(!Boolean(this.keshi)){
          return ''
      }
      // let index = this.keshi;
      let data = this.data;
      console.log(data)
      // 显示
      this.switch1 = Boolean(data.display);
      // 专家
      this.switch2 = Boolean(data.iexpert);
      // 名字
      this.name = data.name;
      if(data.gender == 2){
        this.gender = '女'
      } else if(data.gender == 1){
        this.gender = "男";
      } else if(data.gender == 9){
        this.gender = "其他";
      }
      this.phone = data.phone;
      // 机构名称
      this.yname = data.hospitalname;
      // 科室
      this.keshi = data.deptName;
      // 排序

      if(!data.sort){
        this.isort = ''
      } else {
        this.isort = data.sort;
      }     
      // 特长
      this.expert1 = data.doctorGood;
      // 简介
      this.expert2 = data.personalIntroduction;
      //职称
      this.zhiwu = data.titleName;
      //  职务
      this.post = data.post;
      this.currentId = data.id;
    },
    // 找出选择的医生
    onChange (val) {
      this.deloldDate();
      this.data = val
      this.keshi = '';      
      for(let i=0;i<this.list.length;i++) {
        if(val.name == this.list[i].name) {
          this.keshi = i+1;
        }
      }
      this.currentId = val.id
      this.changes()
    },
    // 查询医生权限管理
    search() {
      if(!Boolean(this.name)){
          return ''
      }
      this.$axios
        .post(api.findDoctorPageList, {
          hospitalId: this.id,
          name: this.name ? this.name.trim() : null,
          pageNo: 1,
          pageSize: 10,
          iexpert: 0,
          authStatus: 2
        })
        .then(res => {
          if (res.data.success) {
            this.list = res.data.object.records;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    onSelect(v) {
      this.deloldDate();
      console.log('onSelect', v);
    },
    back() {
      let pageNo = this.$route.params.pageNo;
      this.functionJS.paramsNavgationTo(this, "expertList", {
        // 公用方法
          pageNo
      }); 
    },
    save() {
      this.disabledSave = true
      let params = {
        // 显示
        display: Number(this.switch1),
        // 排序
        sort: this.isort,
        // 职务
        post: this.post,
        //id
        id: this.currentId,
        // 专家
        iexpert: Number(this.switch2)
      };
      if (this.currentId == -1) {
        this.$Message.error("请选择专家");
        this.disabledSave = false
      } else {
        this.$axios
          .post(api.updateDoctorById, params)
          .then(res => {
            if (res.data.success) {
              let ret = res.data;
              let pageNo = this.$route.params.pageNo;
              setTimeout(() => {
                this.disabledSave = false
                this.functionJS.paramsNavgationTo(this, "expertList", {
                  // 公用方法
                    pageNo
                }); 
              }, 500);
              this.$Message.info("添加成功");
            } else {
              this.$Message.error("添加失败请重试");
              this.disabledSave = false
            }
          })
          .catch(err => {
            this.disabledSave = false
            console.log(err);
          });
      }
    },
    // 重新搜索医生时清空医生信息
    deloldDate(){
      // this.name = '';
      this.phone = '';
      this.gender = '';
      // 显示
      this.switch1 = false;
      // 专家
      this.switch2 = false
      // 名字
      // 机构名称
      this.yname = ''
      // 科室
      this.yKeshi = ''
      this.keshi = ''
      // 排序
      this.isort =''
      // 特长
      this.expert1 = ''
      // 简介
      this.expert2 = ''
      //职称
      this.zhiwu = ''
      //  职务
      this.post =''

      this.currentId = ''
    }
  }
};
</script>

<style scoped lang="less">
.addExpert {
  width: calc(100%);
  padding: 10px 30px;
  margin: 0 auto;
  background: #fff;
  .main_expert {
    width: 98%;
    display: flex;
    flex-direction: column;
    margin: 10px auto;
    border: 1px solid #f0f0f0;
    box-shadow: 0 4px 3px #ebedf8;
    border-radius: 5px;
    margin-bottom: 20px;
    .box{
      padding: 0 20px;
    }
    .title {
      font-size: 20px;
      padding-left: 15px;
      margin-bottom: 20px;
      color: #333;
      height: 40px;
      line-height: 40px;
      background: #ebedf8;         
    }
    .main_expert_item {
      display: flex;
      flex-direction: row;
      height: 50px;
      align-items: center;
      .doctor{
        display: inline-block;
        min-width: 80px;
        padding-right: 10px;
        // border: 1px solid #999;
      }
      .added{
        background-color: #f3f3f310;
      }
      .add{
        color: #07a6a5;
      }
      .main_expert_title {
        min-width: 100px;
        .hongdian{
          margin-left: -5px;
          color: red;
        }
      }
      input {
        display: inline-block;
        width: 400px;
        height: 30px;
        outline: none;
        text-indent: 5px;
      }
      button {
        margin-left: 20px;
      }
    }
    .main_expert_experts {
      display: flex;
      flex-direction: row;
      height: 50px;
      align-items: center;
      .main_expert_title {
        min-width: 100px;
      }
      .iselected {
        select {
          width: 150px;
        }
        span {
          display: inline-block;
          margin: 0 20px;
        }
      }
    }
    .main_expert_inputi {
      display: flex;
      flex-direction: row;
      margin: 10px 0;
      .main_expert_title {
        min-width: 100px;
      }    
      .shuru {
        width: 100%;
        textarea {
          width: 100%;
          height: 150px;
        }
      }
    }
  }
  .expert_save {
      width: 100%;
      margin: 20px auto;
      text-align: center;
    }
}
</style>